<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>点对点</title>
    <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1.5.0/dist/sockjs.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
    <script>
        function initClient(username, success) {
            const token = 'valid_token_example';
            // 创建 SockJS 连接
            const socket = new SockJS('http://localhost:8082/ws?token=' + token);

            // 使用 Stomp 连接，并设置日志级别为 'none'
            const stompClient = Stomp.over(socket);
            stompClient.debug = null;  // 禁用默认的日志输出

            stompClient.connect({
                username: username
            }, function (frame) {
                console.log('[客户端', username, ']连接成功');
                success(stompClient, username);  // 在连接成功后执行操作
            }, function (response) {
                console.log('[客户端', username, ']连接失败:', response);
            });
        }

        initClient("zhangsan", function (client, username) {
            // 订阅自己的频道
            client.subscribe("/user/queue/greeting", function (message) {
                console.log('[客户端', username, ']收到点对点消息:', message.body);
            });

            // 给自己发信息
            // client.send("/app/sendToUser", {}, "哎呦，" + username + "自己给自己发信息咯");

            // 给李四发送信息
            client.send("/app/sendToUser/lisi", {}, "嗨! 李四，我是张三，想和您蕉个朋友");
        });

        initClient("lisi", function (client, username) {
            // 订阅自己的频道
            client.subscribe("/user/queue/greeting", function (message) {
                console.log('[客户端', username, ']收到点对点消息:', message.body);
            });
        });
    </script>
</head>
<body>
<h2>SockJS 测试页面</h2>
</body>
</html>
